<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData&&!filterLoading">
    <!-- 固定区域 品牌 -->
    <div class="item">
      <div class="head">品牌</div>
      <div class="body">
        <a @click="changeBrand(item.id)" :class="{active:item.id===filterData.slectedBrand}" href="javascript:;" v-for="item in filterData.brands" :key="item.id">{{item.name}}</a>
      </div>
    </div>
    <!-- 可筛选的条件 -->
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{item.name}}</div>
      <div class="body">
        <a @click="changeProp(item,prop.id)" :class="{active:prop.id===item.selectAttr}" href="javascript:;" v-for="prop in item.properties" :key="prop.id">{{prop.name}}</a>
      </div>
    </div>
  </div>
  <!-- 骨架效果 加载时（fliterLoading=true）显示 -->
  <div v-else class="sub-fliter">
    <XtxSkeleton class="item" width="800px" height="40px"></XtxSkeleton>
    <XtxSkeleton class="item" width="800px" height="40px"></XtxSkeleton>
    <XtxSkeleton class="item" width="600px" height="40px"></XtxSkeleton>
    <XtxSkeleton class="item" width="600px" height="40px"></XtxSkeleton>
    <XtxSkeleton class="item" width="600px" height="40px"></XtxSkeleton>
  </div>
</template>

<script>
import { ref } from '@vue/reactivity'
import { watch } from '@vue/runtime-core'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category.js'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    const filterLoading = ref(false)
    // 监听地址栏二级类目id的变化，获取筛选数据
    const filterData = ref(null)
    const route = useRoute()
    watch(() => route.params.id, (newval) => {
      // 变化后的id有值且属于二级类目
      if (newval && (`/category/sub/${newval}` === route.path)) {
        filterLoading.value = true
        // 发请求获取数据
        findSubCategoryFilter(route.params.id).then(data => {
          // 1.每一组可选的筛选条件缺失“全部”条件 处理数据加上“全部”
          // 给每一组数据加上当前选中的数据
          data.result.brands.changeBrand = null
          // console.log(data.result.brands)
          data.result.brands.unshift({ id: null, name: '全部' })
          // 2.属性
          data.result.saleProperties.forEach((item) => {
            item.properties.unshift({ id: null, name: '全部' })
            item.selectAttr = null
          })
          // 将数据设置给filterData
          filterData.value = data.result
          // console.log(data.result)
          filterLoading.value = false
        })
      }
    }, { immediate: true })

    // 获取筛选参数的函数
    const getFilterParams = () => {
      const obj = { brandId: null, attrs: [] }
      // 品牌
      obj.brandId = filterData.value.slectedBrand
      // 销售属性
      filterData.value.saleProperties.forEach(item => {
        if (item.selectAttr) {
          const prop = item.properties.find(prop => prop.id === item.selectAttr)
          obj.attrs.push({ groupName: item.name, propertyName: prop.name })
        }
      })
      // 参考数据:{brandId:'',attrs:[{groupName:'',propertyName:''},...]}
      // 空数组不需要发请求
      if (obj.attrs.length === 0) obj.attrs = null
      return obj
    }

    // 记录当前选择的品牌
    const changeBrand = (brandId) => {
      if (filterData.value.slectedBrand === brandId) return
      filterData.value.slectedBrand = brandId
      // console.log(filterData.value.slectedBrand)
      emit('filter-change', getFilterParams())
    }

    // 记录当前选择的销售属性
    const changeProp = (item, propId) => {
      if (item.selectAttr === propId) return
      item.selectAttr = propId
      emit('filter-change', getFilterParams())
    }
    return { filterData, filterLoading, changeBrand, changeProp, getFilterParams }
  }
}
</script>

<style lang="less" scoped>
// 筛选区样式
.sub-filter{
  background-color: #fff;
  padding: 25px;
  .item{
    display: flex;
    line-height: 40px;
    .head{
      width: 80px;
      color:#999;
    }
    .body{
      flex: 1;
      a{
        margin-right: 36px;
        transition: all .3s;
        display: inline-block;
        &.active,&:hover{
          color:@xtxColor;
        }
      }
    }
  }
}
.xtx-skeleton {
      padding: 10px 0;
}
</style>
